<?php 
namespace Home\Model;
use Think\Model;
class PointsLogModel extends Model {
       public function insert($stage,$insertarr,$if_repeat = true){
           $sconfigarr=  $this->setting_config();
            if (!$insertarr['pl_memberid']){
                   return false;
           }
           //记录原因文字
		switch ($stage){
			case 'regist':
				if (!$insertarr['pl_desc']){
					$insertarr['pl_desc'] = '注册会员';
				}
				$insertarr['pl_points'] = intval($sconfigarr['points_reg']);
				break;
			case 'login':
				if (!$insertarr['pl_desc']){
					$insertarr['pl_desc'] = '会员登录';
				}
				$insertarr['pl_points'] = intval($sconfigarr['points_login']);
                                
				break;
			case 'comments':
				if (!$insertarr['pl_desc']){
					$insertarr['pl_desc'] = '评论商品';
				}
				$insertarr['pl_points'] = intval($sconfigarr['points_comments']);
				break;
			case 'order':
				if (!$insertarr['pl_desc']){
					$insertarr['pl_desc'] = '订单'.$insertarr['order_sn'].'购物消费';
				}
				$insertarr['pl_points'] = 0;
				if ($insertarr['orderprice']){ 
					$insertarr['pl_points'] = @intval($insertarr['orderprice']/$sconfigarr['points_orderrate']);
					if ($insertarr['pl_points'] > intval($sconfigarr['points_ordermax'])){
						$insertarr['pl_points'] = intval($sconfigarr['points_ordermax']);
					}
				}
				//订单添加赠送积分列
				$obj_order = M('order_common');
				$data = array();                    
				$data['order_pointscount'] = array('exp','order_pointscount+'.$insertarr['pl_points']);
                                $obj_order->where(array('order_id'=>$insertarr['order_id']))->save($data);
				//$obj_order->editOrderCommon($data,array('order_id'=>$insertarr['order_id']));
				break;
			case 'system':
				break;
			case 'pointorder':
				if (!$insertarr['pl_desc']){
					$insertarr['pl_desc'] = '兑换礼品信息'.$insertarr['point_ordersn'].'消耗积分';
				}
				break;
                        case 'app':
                        if (!$insertarr['pl_desc']){
                                  $insertarr['pl_desc'] = "积分兑换";
                         }
                        //邀请积分返利
				break;
			case 'signin':
				if (!$insertarr['pl_desc']){
					$insertarr['pl_desc'] = '签到得到积分';
				}
                                $insertarr['pl_points'] = intval($sconfigarr['points_signin']);
				break;
//			case 'inviter':
//				if (!$insertarr['pl_desc']){
//					$insertarr['pl_desc'] = '邀请新会员['.$insertarr['invited'].']注册';
//				}
//				$insertarr['pl_points'] = intval($sconfigarr['points_invite']);
//				break;
//			case 'rebate':
//				if (!$insertarr['pl_desc']){
//					$insertarr['pl_desc'] = '被邀请人['.$insertarr['member_name'].']消费';
//				}
//				$insertarr['pl_points'] = $insertarr['rebate_amount'];
//				break;
			case 'other':
				break;
		}
		$save_sign = true;
		if ($if_repeat == false){ 
			//检测是否有相关信息存在，如果没有，入库
			$condition['pl_memberid'] = $insertarr['pl_memberid'];
			$condition['pl_stage'] = $stage;
			$log_array = $this->getPointsInfo($condition,$page);
			if (!empty($log_array)){
				$save_sign = false;
			}
		}
		if ($save_sign == false){
			return true;
		}
             //新增日志
		$value_array = array();
		$value_array['pl_memberid'] = $insertarr['pl_memberid'];
		$value_array['pl_membername'] = $insertarr['pl_membername'];
		if ($insertarr['pl_adminid']){
			$value_array['pl_adminid'] = $insertarr['pl_adminid'];
		}
		if ($insertarr['pl_adminname']){
			$value_array['pl_adminname'] = $insertarr['pl_adminname'];
		}
		$value_array['pl_points'] = $insertarr['pl_points'];
		$value_array['pl_addtime'] = time();
		$value_array['pl_desc'] = $insertarr['pl_desc'];
		$value_array['pl_stage'] = $stage;
		$result = false;
		if($value_array['pl_points'] != '0'){
			$result = $this->addPointsLog($value_array);
		}
		if ($result){
			//更新member内容
			$obj_member = M('member');
			$upmember_array = array();
			$upmember_array['member_points'] = array('exp','member_points+'.$insertarr['pl_points']);
			$obj_member->where(array('member_id'=>$insertarr['pl_memberid']))->save($upmember_array);
			return true;
		}else {
			return false;
		}
           
       }
       	/**
	 * 积分日志详细信息
	 *
	 * @param array $condition 条件数组
	 * @param array $field   查询字段
	 */
	public function getPointsInfo($condition,$field='*'){
		//得到条件语句
		$list		= $this->where($condition)->find();
		return $list;
	}
       	/**
	 * 添加积分日志信息
	 *
	 * @param array $param 添加信息数组
	 */
	public function addPointsLog($param) {
		if(empty($param)) {
			return false;
		}
               
		$result	= $this->add($param);
		return $result;
	}
       public function setting_config(){
           $setting = M("setting");
           $settinginfo = $setting->field("name,value")->select();
           $newarr = array();
           foreach($settinginfo as $setting){
               $newarr[$setting["name"]]=$setting["value"];
           }
           return $newarr;
       }
       

	public function validate(){
		 $id = I('post.id',0,'intval');               //地址id
		 $uid = I('post.uid',0,'intval');             //会员id
		 $true_name = I('post.true_name','');            //会员姓名
		 $area_id = I('post.area_id',0,'intval');     //地区id
		 $city_id = I('post.city_id',0,'intval');     //城市id
		 $address = I('post.address','');                //街道
		 $mob_phone = I('post.mob_phone','');            //手机号
		 $is_default = I('post.is_default') ? 1 : 0;  //是否默认地址
			
		 //print_r(I('post.'));
		 //数据验证
		 if(!$true_name)  returnApiError(1,'请填写会员姓名');
		 if(!$area_id)  returnApiError(1,'地区id参数错误');
		 if(!$city_id)  returnApiError(1,'城市id参数错误');
		 if(!$address)  returnApiError(1,'请填写街道详细地址');
		 if(!$mob_phone)  returnApiError(1,'请填写手机号');
		 if(strlen($true_name) > 50)  returnApiError(1,'姓名长度超过50个字符');
		 if(strlen($address) > 255)  returnApiError(1,'街道地址长度超过255个字符');
		 if(strlen($mob_phone) > 15)  returnApiError(1,'街道地址长度超过15个字符');
		 if(!$this->where("member_id = $uid")->find()) returnApiError(4006,'用户不存在');
		 
		 //实例化地区
		 $Area = M('Area');
		 
		 //市
		 $city_where['area_id'] = $city_id;
		 $city = $Area->where($city_where)->find();
		 $city_name  = $city['area_name'];
		 $pro_id = $city['area_parent_id'];
		 
		 //区
		 $area_where['area_id'] = $area_id;
		 $city = $Area->where($area_where)->find();
		 $area_name  = $city['area_name'];
		 
		 //省
		 $pro_where['area_id'] = $pro_id;
		 $pro = $Area->where($pro_where)->find();
		 $pro_name  = $pro['area_name'];
		 for($i=0;$i<=3;$i++){
			  $txt .= " ";	
		 }
		 $area_info = $pro_name.$txt.$city_name.$txt.$area_name;
		 
		 $where['address_id'] = $id;
		 $where['member_id'] = $uid;
		 $data['true_name'] = $true_name;
		 $data['area_id'] = $area_id;
		 $data['city_id'] = $city_id;
		 $data['address'] = $address;
		 $data['mob_phone'] = $mob_phone;
		 $data['area_info'] = $area_info;
		 $data['member_id'] = $uid;
		 if($is_default)  $data['is_default'] = $is_default;
		
		 $arr = array(
			'data' => $data,
			'where' => $where,
		 );
		 return $arr;
	}
}